package com.wtzz.police.base.controller;

import com.wtzz.police.base.bean.dto.OperationLogDto;
import com.wtzz.police.base.bean.req.OperationLogFilterDto;
import com.wtzz.police.base.entity.OperationLog_;
import com.wtzz.police.base.model.enums.Role;
import com.wtzz.police.base.model.response.PageWapper;
import com.wtzz.police.base.model.response.ResponseBean;
import com.wtzz.police.base.service.OperationLogService;
import com.wtzz.police.base.model.annotaions.RoleCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api(tags = "日志")
@RestController
@RequestMapping("log")
@Validated
public class LogController {

    @Autowired
    OperationLogService service;

    @RoleCheck(Role.ADMIN)
    @PostMapping(value = "list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询日志(管理员权限)", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseBean<PageWapper<OperationLogDto>> listLog(
            @RequestBody
            @Validated
            @NotNull(message = "没提交数据")
            @ApiParam(value = "查询日志条件", type = "body")
                    OperationLogFilterDto filterDto) {
        Page<OperationLogDto> page = service.list(filterDto.createPageRequest(Sort.Direction.DESC, OperationLog_.CREATED_DATE), filterDto);
        return new ResponseBean<>(true, new PageWapper<>(page));
    }

}
